<?php

	

/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_s_reply");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/qq/inc.php");


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		
		echo "tag_count:$count<br>\n";
		$total_page = 100;
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;

		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_mblog_pubu_content = new db_mblog_pic_content();
		if(!$this->start_id) $this->start_id =0;
		//分批次取tag名称
		$mblog_list = $db_mblog_pubu_content->cron_get_list_for_reply_from_sina($p,PCOUNT,$this->start_id);
		if(!$mblog_list)
		{
			exit;
		}
		
		foreach($mblog_list as $value)
		{
			$max_time = "";
			//执行时间超过30分钟，退出
			if(time() - CRON_START_TIME > 3600)
			{
				exit;
			}
			
			$mid = $value["mid"];
			$id = $value["id"];
			$from = $value["from"];
			if($mid)//处理抓取分页逻辑
			{
				if($from == 2)
				{
					$this->get_list_by_topic($id,$mid);
				}
				elseif($from ==1)
				{
					$this->get_list_by_topic_by_qq($id,$mid);
				}
				
				
			}
	
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($id,$mid)
	{
		global $config;
		
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		$db_user_info = new db_user_info();
		$db_mblog_reply = new db_mblog_reply();
		$db_mblog_pic_content = new db_mblog_pic_content();
		if($mid)
		{
			
			//根据话题抓取
			$mblog_list = $mblog_from_api->api_get_comment_by_mid($mid,1,20);
				
			
			echo "{$topic}:count:{$mblog_list["data"]["count"]}|get_count".count($mblog_list["data"]["result"]).":p:{$p}<br>\n";	
			
			if($mblog_list["data"]["result"])
			{
				foreach($mblog_list["data"]["result"] as $value1)
				{
					
					$sina_id = $value1["id"] ;
					$uid = $value1["user"]["id"];
					$user_nickname = $value1["user"]["name"];
					$content = $value1["text"];
					$head_url = $value1["user"]["profile_image_url"];
					$time = $value1["create_time"] ;
					$get_count = $db_mblog_reply->cron_get_count_by_sina_id($sina_id);
					
					if($get_count > 0)
					{
						continue;
						
					}
					$db_mblog_reply->cron_insert_mblog_reply_info($uid,$user_nickname,$id,$content,$sina_id,$head_url,$time);
				}
		
			}
			
			
			$db_mblog_pic_content->cron_update_mblog_count_reply($id);
				
		}
		
			
	}
	

	//根据话题抓取数据并入库
	function get_list_by_topic_by_qq($id,$mid)
	{
		global $config;
		
		$db_base = new db_base();
		$db_user_info = new db_user_info();
		$db_mblog_reply = new db_mblog_reply();
		$db_mblog_pic_content = new db_mblog_pic_content();
		
		$mblog_get_mblog_from_qq = new mblog_get_mblog_from_qq();
		
		if($mid)
		{
			
			//根据话题抓取
			$mblog_list = $mblog_get_mblog_from_qq->get_mblog_reply_by_mid(1,$mid,1,20);
			
			echo "{$topic}:count:{$mblog_list["data"]["count"]}|get_count".count($mblog_list["data"]["result"]).":p:{$p}<br>\n";	
			
			if($mblog_list["data"]["result"])
			{
				foreach($mblog_list["data"]["result"] as $value1)
				{
					$sina_id = $value1["id"] ;
					$uid = $value1["openid"];
					$user_nickname = $value1["nick"];
					$content = $value1["origtext"];
					$head_url = $value1["head"] ? $value1["head"]."/50/" :  "http://mat1.gtimg.com/www/mb/img/p1/head_normal_180.png";
					$time = date("Y-m-d H:i:s",$value1["timestamp"]);
					$get_count = $db_mblog_reply->cron_get_count_by_sina_id($sina_id);
					if($get_count > 0)
					{
						continue;
						
					}
					$db_mblog_reply->cron_insert_mblog_reply_info($uid,$user_nickname,$id,$content,$sina_id,$head_url,$head_url);
				}
		
			}
			
			
			$db_mblog_pic_content->cron_update_mblog_count_reply($id);
		
		}
		
			
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
